﻿@{
    Layout = "~/Views/Shared/_LayuiLayout.cshtml";
}

@model AddressOutput
@using lwzlz.Core;
@{
    var memberList = ViewData["MemberList"] as List<MemberOutput>;
    var deep1List = ViewData["deep1List"] as List<Place>;
    var deep2List = ViewData["deep2List"] as List<Place>;
    var deep3List = ViewData["deep3List"] as List<Place>;
}

<form class="layui-form layui-form-pane">
    @Html.AntiForgeryToken()
    <input type="hidden" value="@Model.Id" name="Id" />
    <div class="layui-form-item">
        <label class="layui-form-label">加盟校账户</label>
        <div class="layui-input-block">
            <select name="MemberId" lay-search lay-filter="MemberId" id="MemberId" lay-verify="required">
                <option value=""></option>
                @{
                    foreach (var item in memberList)
                    {
                        <option value="@item.Id" @(Model.MemberId == item.Id ? "selected" : "")>@item.SchoolName (@item.UserName)</option>

                    }
                }
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="RealName" value="@Model.RealName">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-block">
            <input type="number" class="layui-input" name="Mobile" value="@Model.Mobile">
        </div>
    </div>
    <div class="layui-form-item" style="padding-top:10px;">
        <div class="layui-col-md6">
            <label class="layui-form-label">省份</label>
            <div class="layui-input-block">
                <select name="ProvinceCode" lay-search lay-filter="place" id="ProvinceCode" lay-verify="required">
                    <option value=""></option>
                    @{
                        foreach (var item in deep1List)
                        {
                            <option value="@item.Code" @(Model.ProvinceCode == item.Code ? "selected" : "")>@item.Name</option>

                        }
                    }
                </select>
            </div>
        </div>
        <div class="layui-col-md6">
            <label class="layui-form-label">城市</label>
            <div class="layui-input-block">
                <select name="CityCode" lay-search lay-filter="place" id="CityCode" lay-verify="required">
                    <option value=""></option>
                    @{
                        foreach (var item in deep2List.FindAll(p => p.ParentID == Model.ProvinceCode))
                        {
                            <option value="@item.Code" @(Model.CityCode == item.Code ? "selected" : "")>@item.Name</option>
                        }
                    }
                </select>
            </div>
        </div>
    </div>
    <input type="hidden" name="ProvinceName" value="@Model.ProvinceName" id="ProvinceName" />
    <input type="hidden" name="CityName" value="@Model.CityName" id="CityName" />
    <div class="layui-form-item">
        <label class="layui-form-label">地址</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="SendAddress" value="@Model.SendAddress">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮编</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="PostCode" value="@Model.PostCode">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否默认</label>
        <div class="layui-input-block">
            <input type="radio" name="IsDefault" value="True" title="是" lay-filter="IsDefault" @(Model.IsDefault ? "checked" : "") />
            <input type="radio" name="IsDefault" value="False" title="否" lay-filter="IsDefault" @(!Model.IsDefault ? "checked" : "") />
        </div>
    </div>
    <div class="layui-form-item" align="center">
        <button type="button" class="layui-btn" lay-submit="" lay-filter="Save">保存</button>
        <button type="button" class="layui-btn layui-btn-primary" id="btnCancel">取消</button>
    </div>
</form>

@section scripts{
    <script type="text/javascript">
        var $, layer;
        layui.use(['form'], function () {
            var form = layui.form;
            $ = layui.jquery;
            layer = parent.layer === undefined ? layui.layer : parent.layer;

            // 监听关闭
            $('#btnCancel').on('click', function () {
                layer_close();
            });

            //地区三级联动
            form.on('select(place)', function (data) {
                var urls = '@Url.Action("GetPlace", "Place")' + '?Pid=' + data.value;
                var name = $(data.elem).attr("name");
                if (name == "ProvinceCode") {
                    getJSON(urls, $("#CityCode"));
                    getJSON(urls, $("#DistrictCode"));
                    var ee = $("#ProvinceCode").find("option:selected").text();
                    $("#ProvinceName").val(ee);
                }
                if (name == "CityCode") {
                    getJSON(urls, $("#DistrictCode"));
                    var ee = $("#CityCode").find("option:selected").text();
                    $("#CityName").val(ee);
                }
                //选中内容保存到隐藏域
                $(data.elem).siblings("input:hidden").val($(data.othis).find("dd[class='layui-this']").html());
                console.log($(data.elem).siblings("input:hidden").val());
            });

            function getJSON(urls, even) {
                $.post(urls, function (json) {
                    var name = even.find("select").attr("name");
                    var select = '<select id="' + name + '" name="' + name + '" lay-filter="state" lay-verify="required">';
                    select += '<option value="">请选择</option>';
                    $(json).each(function () {
                        select += '<option value="' + this.Code + '">' + this.Name + '</option>';
                    });
                    select += '</select>';
                    even.html(select);
                    form.render('select');
                });
            };

            var postUrl = '@(Url.Action(Model.Id > 0 ? "Edit" : "Add", "Address"))';

            // 监听提交
            form.on('submit(Save)', function (data) {
                var loading = layer.load();
                $.post(postUrl, data.field,
                    function (result) {
                        layer.close(loading);
                        if (result.Success) {
                            layer.msg(result.Message, { icon: 1, time: 2000 });
                            parent.reload();
                            layer_close();
                        } else {
                            layer.alert(result.Message, { icon: 2 });
                        }
                    });
                return false;
            })

        });
    </script>
}



